package com.we823.project.controller.product;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.we823.project.common.web.status.TaskStatus;
import com.we823.project.controller.AbstractSimpleBaseController;
import com.we823.project.core.web.WebConstants;
import com.we823.project.model.StationModel;
import com.we823.project.model.product.PlanDetailModel;
import com.we823.project.repository.service.StationService;
import com.we823.project.repository.service.product.PlanDetailService;
import com.we823.utils.web.RequestUtil;

@Controller
@RequestMapping(WebConstants.PRODUCT_PLAN_DETAIL)
public class PlanDetailController extends AbstractSimpleBaseController<PlanDetailModel, Integer> {

	private String baseUrl = WebConstants.PRODUCT_PLAN_DETAIL_TEMPLATE;
	
	@Autowired
	private StationService stationService;
	private PlanDetailService planDetailService;
	
	@Autowired
	public PlanDetailController(PlanDetailService planDetailService) {
		super(planDetailService);
		super.baseUrl = baseUrl;
		this.planDetailService = planDetailService;
		setResourceIdentity("task:plan");
	}

	@Override
	public Page<PlanDetailModel> getData(PageRequest pageRequest, HttpServletRequest request, Model model, boolean pageable) {
		Page<PlanDetailModel> planDetailPage = null;
		Integer id = RequestUtil.getValueByParam(request, "id", 0);
		if(id==0){
			if(pageable && pageRequest!=null){
				planDetailPage = planDetailService.findAll(pageRequest);
			}else{
				planDetailPage = new PageImpl<>(planDetailService.findAll());
			}
		}else{
			if(pageable && pageRequest!=null){
				planDetailPage = planDetailService.findByPlanId(id,pageRequest);
			}else{
				planDetailPage = new PageImpl<>(planDetailService.findAll());
			}
		}
		
		fillInfo(planDetailPage);
		return planDetailPage;
	}

	private void fillInfo(Page<PlanDetailModel> planDetailPage) {
		if (planDetailPage!=null) {
			List<PlanDetailModel> planDetailModels = planDetailPage.getContent();
			if (planDetailModels!=null && planDetailModels.size()>0) {
				Map<Integer, StationModel> stationMap = stationService.map();
				for (PlanDetailModel planDetailModel : planDetailModels) {
					if (stationMap.containsKey(planDetailModel.getStationId())) {
						planDetailModel.setStationName(stationMap.get(planDetailModel.getStationId()).getName());
					}
				}
			}
		}
		
	}

	@Override
	public PlanDetailModel getEntity(HttpServletRequest request, Integer id,String oper) {
		PlanDetailModel planDetailModel = null;
		int stationId = RequestUtil.getValueByParam(request, "stationId", 0);
		String memo = RequestUtil.getValueByParam(request, "memo");
		if(id>0) {
			planDetailModel = planDetailService.findOne(id);
			if(planDetailModel!=null) {
				TaskStatus status = planDetailModel.getStatus();
				if ("edit".equals(oper)) {
					//当任务详单还没有被确认或未分配时，可以进行操作；若其他则不可操作
					if(TaskStatus.NoConfirm.equals(status) || TaskStatus.NoAssign.equals(status)) {
						if(TaskStatus.Finished.equals(status)) {
							return planDetailModel;
						}else {
							if(stationId>0) {
								planDetailModel.setStationId(stationId);
								planDetailModel.setStatus(TaskStatus.NoConfirm);
								planDetailModel.setMemo(memo);
							}
						}
					}else {
						
					}
				}else {
					if(TaskStatus.Finished.equals(status)) {
						return planDetailModel;
					}else {
						if(stationId>0) {
							planDetailModel.setStationId(stationId);
							planDetailModel.setStatus(TaskStatus.NoConfirm);
							planDetailModel.setMemo(memo);
						}
					}
				}
				
				
			}
		}
		return planDetailModel;
	}
	

	@RequestMapping("listBy")
	public String list(HttpServletRequest request,Model model) {
		String list = super.list();
		Integer id = RequestUtil.getValueByParam(request, "id", 0);
		model.addAttribute("currentId",id);
		
		List<StationModel> stationModels = stationService.findAll();
		String stations = "0:未分配";
		if(stationModels!=null) {
			int size = stationModels.size();
			for (int index = 0; index < size; index++) {
				StationModel stationModel = stationModels.get(index);
				if(stationModel!=null) {
					if(index==0 && size!=1) {
						stations+=";";
					}
					stations+=stationModel.getId()+":"+stationModel.getName();
					if(index<size-1 && size>1) {
						stations+=";";
					}
				}
			}
		}
		model.addAttribute("stations",stations);
		return list;
	}
	
	@RequestMapping("getInfo")
	@ResponseBody
	public Model getInfo(HttpServletRequest request, Model model) {
		List<StationModel> stationModels = stationService.findAll();
		model.addAttribute("stations",stationModels);
		return model;
	}

	public StationService getStationService() {
		return stationService;
	}

	public void setStationService(StationService stationService) {
		this.stationService = stationService;
	}

}
